package com.cosmoplat.xikai.business.controller.plan;

import com.cosmoplat.xikai.business.domain.dto.productPlan.ProductPlanQueryDTO;
import com.cosmoplat.xikai.business.domain.dto.productPlan.ProductionPlanDTO;
import com.cosmoplat.xikai.business.domain.dto.productPlan.TaskDeliverDTO;
import com.cosmoplat.xikai.business.domain.vo.ProductTaskVo;
import com.cosmoplat.xikai.business.domain.vo.orderTemplate.ProductPlanTemplate;
import com.cosmoplat.xikai.business.service.ProductionPlanService;
import com.cosmoplat.xikai.common.core.controller.BaseController;
import com.cosmoplat.xikai.common.core.domain.AjaxResult;
import com.cosmoplat.xikai.common.core.page.TableDataInfo;
import com.cosmoplat.xikai.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName ProductionPlanController
 * @Description 生产计划
 * @Author wdf
 * @Date 2025/1/7
 */
@Api(tags = "生产计划", value = "生产计划")
@RestController
@Slf4j
@RequestMapping("/productionPlan")
public class ProductionPlanController extends BaseController {

    @Autowired
    private ProductionPlanService productionPlanService;


    /**
     * 查询生产计划列表
     *
     * @param queryDTO
     * @return com.cosmoplat.xikai.common.core.page.TableDataInfo
     * @date 16:22 2025/1/7
     **/
    @ApiOperation(value = "分页查询")
    @PreAuthorize("@ss.hasPermi('business:productionPlan:query')")
    @GetMapping("/queryForPage")
    public TableDataInfo<ProductTaskVo> queryForPage(ProductPlanQueryDTO queryDTO) {
        startPage();
        List<ProductTaskVo> list = productionPlanService.queryForPage(queryDTO);
        return getDataTable(list);
    }

    /**
     * 任务下发
     *
     * @param taskDeliverDTO
     * @return com.cosmoplat.xikai.common.core.domain.AjaxResult
     * @date 16:37 2025/1/7
     **/
    @ApiOperation(value = "任务下发/暂停")
    @PreAuthorize("@ss.hasPermi('business:productionPlan:taskDeliver')")
    @PostMapping("/taskDeliver")
    public AjaxResult taskDeliver(@RequestBody @Validated TaskDeliverDTO taskDeliverDTO) {
        boolean result = productionPlanService.taskDeliver(taskDeliverDTO);
        if (result) {
            return AjaxResult.success();
        }else {
            return AjaxResult.success("未调用成功mes接口");
        }

    }



    /**
     * 下载模板
     *
     * @param response
     * @date 16:40 2025/1/7
     **/
    @ApiOperation(value = "下载模板")
    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.hasPermi('business:productionPlan:import')")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 创建示例数据
        List<ProductPlanTemplate> templateList = ProductPlanTemplate.generateProductPlanTemplates();
        // 使用 RuoYi 的 ExcelUtil 导出 Excel 文件
        new ExcelUtil<>(ProductPlanTemplate.class).exportExcel(response, templateList, "生产计划导入模板", "生产计划导入模板");
    }

    /**
     * 导入实现
     *
     * @param file
     * @return com.cosmoplat.xikai.common.core.domain.AjaxResult
     * @date 16:43 2025/1/7
     **/
    @ApiOperation(value = "导入")
    @PreAuthorize("@ss.hasPermi('business:productionPlan:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ProductPlanTemplate> util = new ExcelUtil<ProductPlanTemplate>(ProductPlanTemplate.class);
        List<ProductPlanTemplate> list = util.importExcel(file.getInputStream(), 1);
        String operName = getUsername();
        String message = productionPlanService.importPlan(list, operName);
        return success(message);
    }
    /**
     * 数据保存
     * @param productionPlanDTO
     * @return com.cosmoplat.xikai.common.core.domain.AjaxResult
     * @date 16:43 2025/1/7
     **/
    @ApiOperation(value = "保存")
    @PreAuthorize("@ss.hasPermi('business:productionPlan:save')")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody  ProductionPlanDTO productionPlanDTO) throws Exception {
        String message = productionPlanService.save(productionPlanDTO);
        return success();
    }
    /**
     * 数据保存
     * @param id
     * @return com.cosmoplat.xikai.common.core.domain.AjaxResult
     * @date 16:43 2025/1/7
     **/
    @ApiOperation(value = "保存")
    @PreAuthorize("@ss.hasPermi('business:productionPlan:byId')")
    @GetMapping("/getbyId")
    public AjaxResult getById(Long id) throws Exception {

        return productionPlanService.getById(id);
    }


}
